home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / util2 / erase.lha / Erase / src.lha / erase.c next >
C/C++ Source or Header  |  1995-08-07  |  5KB  |  240 lines

  1. /***************************************************************************
  2.  * Erase.c
  3.  *
  4.  * Erase -- Its a helluva lot more dangerous than delete :)
  5.  *
  6.  * August 1995, Lee Kindness
  7.  *
  8.  */
  9.  
  10. /* All the includes are in this file */
  11. #include "gst.c"
  12.  
  13. /* Size of the buffer we tack onto the AnchorPath */
  14. #define EXTRABUFLEN 200
  15.  
  16. /* Size of the buffer for fault display */
  17. #define FAULTBUFLEN 1024
  18.  
  19. /* Coment that is written to all files */
  20. #define COMMENT ":-) :-| :-( :-| :-) :-| :-( :-| :-) :-| :-( :-| :-) :-| :-( :-| :-) :-| :-( :-| "
  21.  
  22. /* What to output before the fault text */
  23. #define HEADER "Erase"
  24.  
  25. /* Argument template */
  26. #define TEMPLATE "FILE/M/A,TIMES/K/N"
  27.  
  28. /* Version number */
  29. #define VERSION "1.1"
  30.  
  31. /* Version tag for C:Version to pick up */
  32. char vertag[] = "$VER: Erase "VERSION" "__AMIGADATE__;
  33.  
  34. /* Global preferences: How many times to overwrite a file */
  35. LONG times;
  36.  
  37. /* Prototypes */
  38. void EraseError(STRPTR name, LONG code);
  39. void EraseDir(STRPTR name, struct FileInfoBlock *fib, BPTR parent);
  40. void EraseFile(STRPTR name, struct FileInfoBlock *fib, BPTR parent);
  41. void DoPattern(STRPTR pattern);
  42.  
  43.  
  44. /***************************************************************************
  45.  * EraseError() -- Report an error to the user 
  46.  */
  47. void EraseError(STRPTR name, LONG code)
  48. {
  49.     STRPTR header, buffer;
  50.     if( buffer = AllocVec(FAULTBUFLEN, MEMF_CLEAR) )
  51.     {
  52.         if( !name )
  53.             header = HEADER;
  54.         else
  55.             header = name;
  56.         if( Fault(code, header, buffer, FAULTBUFLEN) )
  57.             Printf("%s\n", buffer);
  58.         else
  59.             if( name) Printf("Cannot erase %s\n", name);
  60.         FreeVec(buffer);
  61.     }
  62. }
  63.  
  64.  
  65. /***************************************************************************
  66.  * EraseDir() -- Erase a directory 
  67.  */
  68. void EraseDir(STRPTR name, struct FileInfoBlock *fib, BPTR parent)
  69. {
  70.     /* Check its not a disk... */
  71.     if( strlen(fib->fib_FileName) ) 
  72.     {
  73.         BPTR olddir;
  74.         struct DateStamp *ds;
  75.         
  76.         olddir = CurrentDir(parent);
  77.         
  78.         /* Clear protection */
  79.         SetProtection(fib->fib_FileName,0);
  80.         
  81.         /* Clear Comment */
  82.         SetComment(fib->fib_FileName, COMMENT);
  83.         
  84.         /* Clear mufs ownership information */
  85.         if( DOSBase->dl_lib.lib_Version >= 39 ) SetOwner(fib->fib_FileName, 0);
  86.         
  87.         /* Clear timestamp */
  88.         if( ds = AllocVec(sizeof(struct DateStamp), MEMF_CLEAR) )
  89.         {
  90.             SetFileDate(fib->fib_FileName, ds);
  91.             FreeVec(ds);
  92.         }
  93.         
  94.         /* Delete the directory */
  95.         if( DeleteFile(fib->fib_FileName) )
  96.             Printf("%s erased\n", name);
  97.         else
  98.             EraseError(name, IoErr());
  99.         
  100.         CurrentDir(olddir);
  101.     }
  102. }
  103.  
  104.  
  105. /***************************************************************************
  106.  * EraseFile() -- Erase a file 
  107.  */
  108. void EraseFile(STRPTR name, struct FileInfoBlock *fib, BPTR parent)
  109. {
  110.     BPTR olddir;
  111.     struct DateStamp *ds;
  112.     LONG i, n;
  113.     BPTR file;
  114.     
  115.     olddir = CurrentDir(parent);
  116.     
  117.     /* Clear protection */
  118.     SetProtection(fib->fib_FileName,0);
  119.     
  120.     /* Clear Comment */
  121.     SetComment(fib->fib_FileName, COMMENT);
  122.     
  123.     /* Clear mufs ownership information */
  124.     if( DOSBase->dl_lib.lib_Version >= 39 ) SetOwner(fib->fib_FileName, 0);
  125.     
  126.     /* Clear timestamp */
  127.     if( ds = AllocVec(sizeof(struct DateStamp), MEMF_CLEAR) )
  128.     {
  129.         SetFileDate(fib->fib_FileName, ds);
  130.         FreeVec(ds);
  131.     }
  132.     
  133.     /* Clear file contents 'times' times */
  134.     for(n = 0; n < times; n++) {
  135.         if( file = Open(fib->fib_FileName, MODE_OLDFILE) ) {
  136.             Seek(file, 0, OFFSET_BEGINING);
  137.             for(i = 0; i < fib->fib_Size; i++)
  138.                 FPutC(file, n);
  139.             Close(file);
  140.         }
  141.     }
  142.     
  143.     /* Set file size to zero */
  144.     if( file = Open(fib->fib_FileName, MODE_OLDFILE) ) {
  145.         SetFileSize(file, OFFSET_BEGINING, 0);
  146.         Close(file);
  147.     }
  148.     
  149.     /* Delete the file */
  150.     if( DeleteFile(fib->fib_FileName) )
  151.         Printf("%s erased\n", name);
  152.     else
  153.         EraseError(name, IoErr());
  154.     
  155.     CurrentDir(olddir);
  156. }
  157.  
  158.     
  159. /***************************************************************************
  160.  * DoPattern() -- Parse and traverse the pattern 
  161.  */
  162. void DoPattern(STRPTR pattern)
  163. {
  164.     struct AnchorPath *ap;
  165.     if( ap = AllocVec(sizeof(struct AnchorPath) + EXTRABUFLEN, MEMF_CLEAR) )
  166.     {
  167.         LONG ret;
  168.         ap->ap_BreakBits = SIGBREAKF_CTRL_C;
  169.         ap->ap_Strlen = EXTRABUFLEN;
  170.         ap->ap_Flags = APF_DOWILD;
  171.         
  172.         ret = MatchFirst(pattern, ap);
  173.         while( !ret )
  174.         {
  175.             if( ap->ap_Info.fib_DirEntryType > 0 )
  176.             {
  177.                 if( !(ap->ap_Flags & APF_DIDDIR) )
  178.                     ap->ap_Flags |= APF_DODIR;
  179.                 else
  180.                 {
  181.                     ap->ap_Flags &= !APF_DIDDIR;
  182.                     EraseDir((STRPTR)&ap->ap_Buf, &ap->ap_Info, ap->ap_Current->an_Lock);
  183.                 }
  184.             } else
  185.                 EraseFile((STRPTR)&ap->ap_Buf, &ap->ap_Info, ap->ap_Current->an_Lock);
  186.  
  187.             ret = MatchNext(ap);
  188.         }
  189.         MatchEnd(ap);
  190.         ret = IoErr();
  191.         if( ret != ERROR_NO_MORE_ENTRIES )
  192.             EraseError(NULL, ret);
  193.         
  194.         FreeVec(ap);
  195.     }
  196. }
  197.  
  198.  
  199. /***************************************************************************
  200.  * main() --
  201.  */
  202. int main(int argc, char **argv)
  203. {
  204.     struct RDArgs *rdargs;
  205.     LONG ret;
  206.     #define OPT_FILE 0
  207.     #define OPT_TIMES 1
  208.     LONG args[2] = {0, 0};
  209.     /* Parse the argument template */
  210.     if (rdargs = ReadArgs(TEMPLATE, (LONG *)&args, NULL)) {
  211.         STRPTR s;
  212.     
  213.         if( args[OPT_TIMES] ) {
  214.             times = *((LONG *)args[OPT_TIMES]);
  215.         } else
  216.             times = 1;
  217.         if( times < 0 )
  218.             times = 1;
  219.         
  220.         s = *((STRPTR *)args[OPT_FILE]);
  221.         while( (s) && (*s) ) 
  222.         {
  223.             /* Act on the pattern */
  224.             DoPattern(s);
  225.             
  226.             /* Get the next string in the array */
  227.             s = strchr(s,'\0');
  228.             s++;
  229.         }
  230.         
  231.         FreeArgs(rdargs);
  232.         ret = 0;
  233.     } else
  234.         ret = 20;
  235.     
  236.     if( ret )
  237.         EraseError(NULL, IoErr());
  238.  
  239.     return ret;
  240. }